<template>
  <div class="layout_nav">
    <div class="nav_menu">
      <el-menu
          active-text-color="#ffd04b"
          background-color="#545c64"
          router
          :default-openeds="defaultOpeneds"
          :default-active="defaultActive"
          text-color="#fff"
      >
        <template v-for="menu in navList">
          <el-menu-item v-if="menu.children.length === 0" :index="menu.path">
            <svg class="icon icon_svg" aria-hidden="true">
              <use :xlink:href="'#' + menu.icon"></use>
            </svg>
            <span>{{ menu.name }}</span>
          </el-menu-item>
          <el-sub-menu v-else :index="menu.path">
            <template #title>
              <svg class="icon_svg" aria-hidden="true">
                <use :xlink:href="'#' + menu.icon"></use>
              </svg>
              <span>{{ menu.name }}</span>
            </template>
            <el-menu-item v-for="subMenu in menu.children" :index="subMenu.path">
              <svg class="icon_svg" aria-hidden="true">
                <use :xlink:href="'#' + subMenu.icon"></use>
              </svg>
              {{ subMenu.name }}
            </el-menu-item>
          </el-sub-menu>
        </template>
      </el-menu>
    </div>
    <div class="nav_view">
      <router-view></router-view>
    </div>
  </div>
</template>

<script setup>
import {ref, computed, watch} from "vue";
import router from "@/router";

const props = defineProps({
  navList: {
    type: Array,
    default: []
  }
})
watch(() => router.currentRoute.value.path, (toPath) => {
  if (toPath === '/comlibrary') {
    let firstNav = ''
    firstNav = props.navList[0].children.length === 0 ? props.navList[0].path : props.navList[0].children[0].path
    router.push(firstNav)
  }
}, {immediate: true, deep: true})

const defaultOpeneds = computed(() => {
  let allOpends = []
  props.navList.forEach(nav=>{
    if (nav.children.length !== 0) allOpends.push(nav.path)
  })
  return allOpends
})
const defaultActive = computed(()=>{
  return router.currentRoute.value.path
})
</script>

<style scoped lang="less">
.layout_nav {
  width: 100%;
  height: 100%;
  display: flex;
  overflow: hidden;
  .nav_menu {
    width: 12.5%;
    min-width: 200px;
    height: 100%;
    overflow: auto;
    background-color: #545C64;
  }
  .nav_view {
    width: 87.5%;
    height: 100%;
    overflow: auto;
  }
  .el-menu-item.is-active{
    background-color: #282828;
  }
}
.icon_svg {
  display: block;
  width: 18px;
  height: 18px;
  margin-right: 6px;
}
</style>